import { App, createVNode } from "vue";
import request from "./../utils/request";
import { useAppStore } from "./../store/app";
import { ElMessage } from "element-plus";
import * as Icons from "@element-plus/icons";
import directive from "@/directive/index";

export default function (_vue: App) {
  _vue.config.globalProperties.$http = {
    get: function (url: string, params: Object, config = {}) {
      return request.get(url, config).catch((err) => {
        ElMessage.error(err.message || err.msg || "请求异常");
      });
    },
    post: function (url: string, params: Object, config = {}) {
      return request.post(url, params, config).catch((err) => {
        ElMessage.error(err.message || err.msg || "请求异常");
      });
    },
  };

  // _vue.config.globalProperties.$loadingState = (
  //   isLoading = false,
  //   message = ""
  // ) => {
  //   useAppStore().loadingState(isLoading, message);
  // };

  //指令集
  directive(_vue);

  // component方法注册的组件都是全局的
  // 注册icon组件  <Icon :name="name" :size="24"></Icon>
  _vue.component("Icon", Icon);
}
// 创建Icon组件
const Icon = (props: { icon: string }) => {
  const { icon } = props;
  return createVNode(Icons[icon as keyof typeof Icons]);
};
